package com.ruoyi.project.cms.task.service;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.cms.task.domain.Task;
import com.ruoyi.project.cms.task.mapper.TaskMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TaskServiceImpl implements ITaskService {
    private static final Logger log = LoggerFactory.getLogger(TaskServiceImpl.class);

    @Autowired
    private TaskMapper taskMapper;
    @Override
    public List<Task> selectTaskList(Task task) {
        return taskMapper.selectTaskList(task);
    }

    @Override
    public Task selectTaskBytaskTitle(String title) {
        return taskMapper.selectTaskByTitle(title);
    }

    @Override
    public Task selectTaskById(Integer taskId) {
        return taskMapper.selectTaskById(taskId);
    }

    @Override
    public int deleteTaskById(Integer taskId) {
        return taskMapper.deleteTaskById(taskId);
    }

    @Override
    public int updateTask(Task task) {
        task.setSaleNameId(taskMapper.selectsalesId(task.getName()));
        return taskMapper.updateTask(task);
    }

    @Override
    public int insertTask(Task task) {
        task.setSaleNameId(taskMapper.selectsalesId(task.getName()));
        return taskMapper.insertTask(task);
    }

    @Override
    public int selectgoodsCount(String saleName) {
        System.out.println("****************商品库存数量"+taskMapper.selectgoodsCount(saleName));
        return taskMapper.selectgoodsCount(saleName);
    }

    /**
     * 导入任务数据
     * @param taskList
     * @param updateSupport
     * @return
     */
    @Override
    public String importTask(List<Task> taskList, boolean updateSupport) {
        if (StringUtils.isNull(taskList) || taskList.size() == 0)
        {
            throw new BusinessException("导入任务数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        String operName = ShiroUtils.getLoginName();
        for (Task task : taskList)
        {
            try
            {
                // 验证是否存在这个任务
                Task t = taskMapper.selectTaskByTitle(task.getTitle());
                if (StringUtils.isNull(t))
                {
                    this.insertTask(task);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + task.getTitle() + " 导入成功");
                }
                else if (updateSupport)
                {
                    task.setUpdateBy(operName);
                    this.updateTask(task);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + task.getTitle() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + task.getTitle() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + task.getTitle() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
